from wtforms import Form, StringField, PasswordField, ValidationError
from wtforms.validators import length, DataRequired, Email, EqualTo
from app.models.user import User


class RegisterForm(Form):
    email = StringField(validators=[DataRequired(message='邮件不能为空'), length(8, 64),
                                    Email(message='邮件格式错误')])

    password = PasswordField(validators=[
        DataRequired(message='密码不能为空'), length(6, 32)])

    nickname = StringField(validators=[
        DataRequired(message='昵称不能为空'), length(2, 10, message='昵称至少两个字符,最多十个字符')])


    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('mail 重复')


    def validate_nickname(self, field):
        if User.query.filter_by(nickname=field.data).first():
            raise ValidationError('nickname 重复')


class LoginForm(Form):
    email = StringField(validators=[DataRequired(message='邮件不能为空'), length(8, 64),
                                    Email(message='邮件格式错误')])

    password = PasswordField(validators=[
        DataRequired(message='密码不能为空'), length(6, 32, message='密码长度请在6-32个字符之间')])


class ForgetPasswordForm(Form):
    email = StringField(validators=[DataRequired(message='邮件不能为空'), length(8, 64),
                                    Email(message='邮件格式错误')])


class ResetPasswordForm(Form):
    password1 = PasswordField(validators=[DataRequired(message='请输入密码'),
                                         length(6, 20, message='密码长度在6-20个字符之间'),
                                         EqualTo('password2', message='两次密码不一致')])

    password2 = PasswordField(validators=[DataRequired(message='请输入密码'),
                                                   length(6, 20, message='密码长度在6-20个字符之间')])